package com.lxl.bt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lxl.bt.Constant;
import com.lxl.bt.R;
import com.lxl.bt.bean.LoginBody;
import com.lxl.bt.bean.ServerCityBeanListBean;
import com.lxl.bt.net.NetApi;
import com.lxl.bt.net.Result;
import com.lxl.bt.retorfit.NetCallback;
import com.lxl.bt.retorfit.RetrofitUtil;
import com.lxl.bt.util.ACache;
import com.lxl.bt.util.Prefs;
import com.lxl.bt.util.ToastUtil;
import com.yanzhenjie.sofia.Sofia;

import java.util.ArrayList;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  登录
 * on  2020年12月3日20:43:33
 **/
public class LoginActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etPhone)
    EditText etPhone;
    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.tvRegist)
    TextView tvRegist;
    @BindView(R.id.tvCommit)
    TextView tvCommit;
    @BindView(R.id.tvForget)
    TextView tvForget;
    private Context context;
    ACache aCache;
    private ArrayList<ServerCityBeanListBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<ServerCityBeanListBean>> options2Items = new ArrayList<>();
    private OptionsPickerView pvOptions;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("");
        aCache = ACache.get(this);
//        tvCardno.setText(Prefs.getStringValue(Constant.CARDNO));
        etPhone.setText(Prefs.getStringValue(Constant.PHONE));
        if (Prefs.getBooleanValue(Constant.SAVE_PASS)) {
            etPassword.setText(Prefs.getStringValue(Constant.PASSWORD));
        }
    }



    private void land() {
        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            ToastUtil.showToast(this, "请输入账号");
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString())) {
            ToastUtil.showToast(this, "请输入密码");
            return;
        }
        // "code": "string",
        //  "password": "string",
        //  "phone": "string",
        //  "visCode": "string"
        showProgressDialog("加载中...");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("phone", etPhone.getText().toString());
        jsonObject.addProperty("password", etPassword.getText().toString());
        RetrofitUtil.getInstance().create(NetApi.class, 1)
                .loginByPhone(jsonObject).enqueue(new NetCallback<Result<ArrayList<LoginBody>>>() {
            @Override
            public void onSuccess(Result<ArrayList<LoginBody>> result) {
                Log.w("lxl", ">>>>" + new Gson().toJson(result));
                hideProgressDialog();
                if (result.isSuccess()) {
                    Prefs.savePre(Constant.PHONE, etPhone.getText().toString());
                    Prefs.savePre(Constant.PASSWORD, etPassword.getText().toString());
                    Prefs.savePre(Constant.VISTORCODE,result.getData().get(0).getUser().getUser().getVistorCode());
                    aCache.put(Constant.LOGIN, result.getData().get(0));
                    Prefs.savePre(Constant.JOB_ID,result.getData().get(0).getUser().getUser().getJob().getId());
                    Prefs.savePre(Constant.JSESSIONID, result.getData().get(0).getToken());
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    finish();
                } else {
                    ToastUtil.showToast(LoginActivity.this, result.getMsg());
                }

            }

            @Override
            public void netLogin() {

            }

            @Override
            public void onError(String errorStr) {
                super.onError(errorStr);
                ToastUtil.showToast(LoginActivity.this, errorStr);
                hideProgressDialog();
            }

        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.rlBack, R.id.tvRegist, R.id.tvCommit, R.id.tvForget})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                break;
            case R.id.tvRegist:
                startActivity(new Intent(this, ResetActivity.class));
                break;
            case R.id.tvCommit:
                land();
//                startActivity(new Intent(this, HomeActivity.class));
                break;
            case R.id.tvForget:
                startActivity(new Intent(this, ForgetPasswordActivity.class));
                break;
        }
    }
}
